﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using SageKing.OpenIddict.UI.Identity.Core;
using SageKing.OpenIddict.UI.Infrastructure.Features;

namespace SageKing.Extensions;

public static class ModuleExtensions
{
    public static IModule UseOpenIddictUIIdentityCoreServer(this IModule module, Action<OpenIddictUIIdentityCoreFeature>? configure = default)
    {
        module.Configure<OpenIddictUIIdentityCoreFeature>(feature =>
        {
            configure?.Invoke(feature);
        });

        module.UseIceMediatR(o => o.MediatRServiceConfiguration += a =>
        {
            a.RegisterServicesFromAssembly(typeof(OpenIddictUIIdentityCoreFeature).Assembly);
        });


        return module;
    }

    public static IServiceCollection AddOpenIddictUIIdentityCoreServices<TApplicationUser, TKey>(
   this IServiceCollection services
 )
   where TKey : IEquatable<TKey>
   where TApplicationUser : IdentityUser<TKey>, new()
    {
        services.AddTransient<IClaimTypeService, ClaimTypeService>();
        services.AddTransient<IUserCreationStrategy<TApplicationUser, TKey>, EmailUserCreationStrategy<TApplicationUser, TKey>>();

        return services;
    }

    /// <summary>
    /// Registers the UserName to UserName UserCreationStrategy.
    /// </summary>
    public static IServiceCollection AddUserNameUserCreationStrategy<TApplicationUser, TKey>(
      this IServiceCollection services
    )
      where TKey : IEquatable<TKey>
      where TApplicationUser : IdentityUser<TKey>, new()
    {
        services
          .AddTransient<IUserCreationStrategy<TApplicationUser, TKey>, UserNameUserCreationStrategy<TApplicationUser, TKey>>();

        return services;
    }

}
